package cc.linker.steplink.crm.configuration;

import cc.linker.steplink.crm.domain.User;
import cc.linker.steplink.crm.service.IUserService;
import lombok.extern.slf4j.Slf4j;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;
import org.aspectj.lang.reflect.MethodSignature;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
import org.springframework.util.StringUtils;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.LinkedHashMap;
import java.util.Map;

/**
 * Controller层监控，目前只打印日志
 */
@Slf4j
//@Aspect
//@Configuration
public class ControllerMonitorAspectConfiguration {

    //    @Autowired
    private IUserService userService;

//    @Pointcut("execution(* cc..*.controller..*.*(..))")
//    private void monitorPoint() {
//
//    }

    //    @Around("monitorPoint()")
    public Object around(ProceedingJoinPoint pjp) throws Throwable {
        ServletRequestAttributes attributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
        if (attributes == null) {
            return pjp.proceed();
        }
        HttpServletRequest request = attributes.getRequest();

        String usercode = request.getRemoteUser();

        HttpSession session = request.getSession();
        String usercode1 = (String) session.getAttribute("usercode");
        if (usercode != null && !usercode.equals(usercode1)) {
            User user = userService.getSimpleUser(usercode);
            session.setAttribute("usercode", usercode);
            session.setAttribute("adminId", user.getAdminId());
            session.setAttribute("orgId", user.getOrgId());
            session.setAttribute("adminName", user.getAdminName());
            // 机构支付模式
            Integer payMode = userService.getPayMode(user.getOrgId());
            session.setAttribute("payMode", payMode);
            Map<String,String> customerMap = userService.isSaler(user.getAdminId());
            session.setAttribute("ifSales", customerMap==null ? false : true);
            session.setAttribute("userinfo", user);
        }
        return pjp.proceed();
//        MethodSignature signature = (MethodSignature) pjp.getSignature();
//        String[] parameterNames = signature.getParameterNames();
//        Object[] args = pjp.getArgs();
//        Map<String, Object> map = new LinkedHashMap<>();
//        int i = 0;
//        for (Object arg : args) {
//            map.put(parameterNames[i], arg);
//            i++;
//        }
//        long start = System.currentTimeMillis();
//        try {
//            return pjp.proceed();
//        } finally {
//            long end = System.currentTimeMillis();
//            // 监听服务器状态的接口不计入日志
//            if (!"/crm/hello".equals(request.getRequestURI())) {
//                LOGGER.info("Request Address：" + request.getRequestURI() + "\n" +
//                        "Method parameters：" + map + "\n" +
//                        "Method execution time：" + (end - start) + "ms,usercode=" + usercode);
//            }
//        }

    }

}
